<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属类型" prop="typeId">
        <el-select v-model="queryParams.typeId" placeholder="请选择所属类型" clearable>
          <el-option
            v-for="item in bankTypeList"
            :key="item.id"
            :label="item.pdfBaseName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="户名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-select v-model="bankTypeId" placeholder="请选择所属类型" size="mini" clearable>
          <el-option
            v-for="item in bankTypeList"
            :key="item.id"
            :label="item.pdfBaseName"
            :value="item.id"
          />
        </el-select>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="addBankPdf"
          v-hasPermi="['system:cfile:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:cfile:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cfileList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="所属类型" align="center" prop="typeId" />-->
      <el-table-column label="户名" align="center" prop="userName"/>
      <el-table-column label="账户号" align="center" prop="accountNo">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.inNum === 0" type="primary">{{scope.row.accountNo}}</el-tag>
          <span v-else>{{scope.row.accountNo}}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="电子流水单" align="center" prop="serialNo" />-->
      <el-table-column label="开始时间/结束时间" align="center" prop="startTime">
        <template slot-scope="scope">
          {{scope.row.startTime}} / {{scope.row.endTime}}
        </template>
      </el-table-column>
<!--      <el-table-column label="结束时间" align="center" prop="endTime"/>-->
      <el-table-column label="总金额(收)" align="center" prop="totalInMoney"/>
      <el-table-column label="总金额(支)" align="center" prop="totalOutMoney"/>
      <el-table-column label="收支差值" align="center" prop="totalDiffMoney"/>
      <!--      <el-table-column label="备注信息" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="openDownLoadBox(scope.row.id)"
            v-hasPermi="['system:cfile:edit']"
          >下载
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:cfile:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="bankTypeTitle" :visible.sync="openBankTypeComponents" width="70%" append-to-body>
      <nh-pdf v-if="bankTypeId === 1" @createPdfResp="createPdfResp" ref="nhPdf"/>
      <jh-pdf v-if="bankTypeId === 2" @createPdfResp="createPdfResp" ref="jhPdf"/>
      <wx-pdf v-if="bankTypeId === 3" @createPdfResp="createPdfResp" ref="wxPdf"/>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="scrollSonToTop">顶部</el-button>
        <el-button size="mini" @click="cancelBankTypeDiglog">关闭</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {delCfile, listCfile} from "@/api/system/cfile";
import {listAllBankType} from '@/api/system/base';
import jhPdf from '@/views/components/pdf/jhPdf.vue';
import nhPdf from '@/views/components/pdf/nhPdf.vue';
import wxPdf from '@/views/components/pdf/wxPdf.vue';

export default {
  name: "Cfile",
  dicts: ['align_type'],
  components: {
    jhPdf,
    nhPdf,
    wxPdf
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // PDF文件生成记录表格数据
      cfileList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      bankTypeId: '',
      openBankTypeComponents: false,
      bankTypeTitle: '前置条件填写',
      bankTypeList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        typeId: null,
        userName: null,
        accountNo: null
      }
    };
  },
  created() {
    this.getBankTypeList();
    this.getList();
  },
  methods: {
    // 获取所有银行信息
    getBankTypeList() {
      listAllBankType().then(res => {
        this.bankTypeList = res.data;
      })
    },
    /** 查询PDF文件生成记录列表 */
    getList() {
      this.loading = true;
      listCfile(this.queryParams).then(response => {
        this.cfileList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancelBankTypeDiglog() {
      this.openBankTypeComponents = false;
      this.$refs.jhPdf.reset();
      this.$refs.nhPdf.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        typeId: null,
        userName: null,
        accountNo: null,
        serialNo: null,
        startTime: null,
        endTime: null,
        originalMoney: null,
        detailNums: null,
        inNum: null,
        totalInMoney: null,
        totalOutMoney: null,
        totalDiffMoney: null,
        inMoneyDetail: null,
        outMoneyDetail: null,
        pdfFileName: null,
        pdfFileDownloadUrl: null,
        createTime: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 生成PDF按钮
    addBankPdf() {
      if (this.bankTypeId !== null && this.bankTypeId !== '' && this.bankTypeId !== undefined) {
        this.openBankTypeComponents = true;
      } else {
        this.$modal.msgError("bank类型不可为空!");
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除PDF文件生成记录编号为"' + ids + '"的数据项？').then(function () {
        return delCfile(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    openDownLoadBox(fileId) {
      this.$confirm('点击下载文件', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'success'
      }).then(() => {
        this.downLoadFile(fileId);
      });
    },
    // 下载处理完成的文件
    downLoadFile(fileId) {
      this.$download.downloadDealFile(fileId).then(res => {
        this.$modal.msgSuccess("文件下载中,请稍等...");
      })
    },
    createPdfResp(res) {
      if (res.code === 200) {
        this.$modal.msgSuccess("文件创建成功!")
        this.getList();
        this.openBankTypeComponents = false;
      } else {
        this.$modal.msgError(res.msg);
      }
    },
    // 调用子组件置顶
    scrollSonToTop() {
      this.$refs.jhPdf.scrollToTop();
    }
  }
};
</script>
